//
//  ICMCarePackageView.m
//  ICrEAM
//
//  Created by rainimute on 16/10/27.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMCarePackageView.h"
#import "ICMCarePackageCell.h"
#import "ICMSeleteItemView.h"
#import "ICMCarePackageModel.h"
#import "ICMCarePackageDownModel.h"
#import "ChoseMenuViewController.h"


@interface ICMCarePackageView()<UITableViewDelegate,UITableViewDataSource,ICMCareItemCellDelegate>{
    NSString *_oilValue;
    
}
@property (nonatomic, strong)NSMutableArray *preArr;
@property (nonatomic, strong) UITableView *carePackageTableView;

@property (nonatomic, strong) UITableView *careItemTableView;

@property (nonatomic, strong) UIView *easyBreakView;

@property (nonatomic, strong) UIView *deepCareView;

@property (nonatomic, assign) NSInteger packageSelectedIndex;//选中的序号

@property (nonatomic, strong) NSMutableArray *packageSelectedItems;//选中的序号

@property (nonatomic, strong) TPKeyboardAvoidingScrollView *wholeScrollView;

@property (nonatomic, strong) NSMutableArray *easyBreakViews;

@property (nonatomic, strong) NSMutableArray *deepCareViews;

@property (nonatomic, copy) NSString *itemInputText;

@property (nonatomic, assign) NSInteger oldSelected;
@property(nonatomic,strong)NSArray *choseArr;

@end

@implementation ICMCarePackageView

#pragma mark    初始化
- (instancetype)initCarePackageViewWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        self.careSelectedItemIndexs = [NSMutableArray array];
        self.packageSelectedItems = [NSMutableArray array];
        self.packageSelectedIndex = -1;
        self.oldSelected = 0;
        
        self.backgroundColor = BackgroundColor_White;
        UILabel *careStyleLabel = [[UILabel alloc]initWithFrame:CGRectMake(ActualWidth(15), ActualHeight(132.5), ActualWidth(40), ActualHeight(60))];
        [careStyleLabel setUpLabelWithText:@"套餐\n类型" alignment:NSTextAlignmentCenter textSize:18 textColor:TitleColor];
        careStyleLabel.numberOfLines = 0;
        careStyleLabel.lineBreakMode = NSLineBreakByWordWrapping;
        [self.wholeScrollView addSubview:careStyleLabel];
        //
        self.easyBreakViews = [NSMutableArray array];
        NSArray *easyBreakTitles = @[@"雨刮",@"刹车片",@"轮胎",@"其他"];
        for (NSInteger i=0; i<easyBreakTitles.count; i++) {
            [self addSelecteItemViewWithTitle:easyBreakTitles[i] isHaveInput:i>0 sectionTitle:@"易损件" changeX:12.5+ActualWidth(170)*i+ActualWidth(75) isEasyBreak:YES];
        }
        self.deepCareViews = [NSMutableArray array];
        NSArray *deepCareTitles = @[@"发动机润滑",@"燃油系统养护",@"进气系统养护",@"空调系统养护"];
        for (NSInteger i=0; i<deepCareTitles.count; i++) {
            [self addSelecteItemViewWithTitle:deepCareTitles[i] isHaveInput:NO sectionTitle:@"深度养护" changeX:10+ActualWidth(170)*i+ActualWidth(75) isEasyBreak:NO];
        }
        //增加分割线
        UIView *sepWidthView1 = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMinY(self.easyBreakView.frame), ScreenWidth, 1)];
        sepWidthView1.backgroundColor = SepColor_gray;
        [self.wholeScrollView addSubview:sepWidthView1];
        UIView *sepWidthView2 = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMinY(self.deepCareView.frame), ScreenWidth, 1)];
        sepWidthView2.backgroundColor = SepColor_gray;
        [self.wholeScrollView addSubview:sepWidthView2];
        UIView *sepHeightView = [[UIView alloc]initWithFrame:CGRectMake(ActualWidth(74), 0, 1, ScreenHeight)];
        sepHeightView.backgroundColor = SepColor_gray;
        [self.wholeScrollView addSubview:sepHeightView];
        UIView *sepHeightView2 = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.careItemTableView.frame), 0, 1, ActualHeight(350))];
        sepHeightView2.backgroundColor = SepColor_gray;
        [self.wholeScrollView addSubview:sepHeightView2];
        
        self.carePackageTableView.hidden = NO;
        
    }
    return self;
}

+ (instancetype)createCarePackageViewWithFrame:(CGRect)frame{
    return [[ICMCarePackageView alloc]initCarePackageViewWithFrame:frame];
}
#pragma mark    添加易损件等输入选择项
- (void)addSelecteItemViewWithTitle:(NSString *)title isHaveInput:(BOOL)isHaveInput sectionTitle:(NSString *)sectionTitle changeX:(float)changeX isEasyBreak:(BOOL)isEasyBreak{
    ICMSeleteItemView *selecteItemView = [ICMSeleteItemView creatSeleteItemViewWithTitle:title isHaveInput:isHaveInput sectionTitle:sectionTitle];
    [selecteItemView changeXToOtherX:changeX];
    if (isEasyBreak) {
        [self.easyBreakView addSubview:selecteItemView];
        [self.easyBreakViews PACA_AddObject:selecteItemView];
        [selecteItemView centerYInSuperView:self.easyBreakView];
    }else{
        [self.deepCareView addSubview:selecteItemView];
        [self.deepCareViews PACA_AddObject:selecteItemView];
        [selecteItemView centerYInSuperView:self.deepCareView];
    }
}

#pragma mark    tableViewDeleage、dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.packages.count) {
        ICMCarePackageModel *carePageModel = self.packages[self.packageSelectedIndex<0?self.oldSelected:self.packageSelectedIndex];
        return tableView == self.carePackageTableView?self.packages.count:carePageModel.TT_RECPTION_PACKAGE_ITEM.count+1;
    } else {
        return 0;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BOOL isPackage = tableView == self.carePackageTableView;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:isPackage?@"ICMCarePackageCell":@"ICMCareItemCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (isPackage) {
        ICMCarePackageCell *cell =[tableView dequeueReusableCellWithIdentifier:@"ICMCarePackageCell"];
        if (!cell) {
            cell = [[ICMCarePackageCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"ICMCarePackageCell"];
        }
        ICMCarePackageModel *carePageModel = self.packages[indexPath.row];
        [cell setupCarePackageCellWithSelectedState:indexPath.row == self.packageSelectedIndex andTitle:carePageModel.PACKAGE_NAME];
        return cell;
    }else{
        ICMCareItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ICMCareItemCell"];
        if (!cell) {
            cell = [[ICMCareItemCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"ICMCareItemCell"];
        }
        cell.delegate = self;
        @weakify(self);
        cell.vehicleBlock=^(){
              @strongify(self);
            [self popCancelResonWithindex:indexPath.row];
        };
        ICMCarePackageModel *carePageModel = self.packages[self.packageSelectedIndex<0?self.oldSelected:self.packageSelectedIndex];
        BOOL isLast = indexPath.row==carePageModel.TT_RECPTION_PACKAGE_ITEM.count;
      
   
        if (isLast) {
     
            [cell setupCareItemCellWithSelectedState:[self.careSelectedItemIndexs containsObject:[@(indexPath.row) stringValue]] andTitle:nil isHaveSubTitle:NO andSubTitle:nil isLast:YES ishideBtn:YES andValue:nil];
            cell.itemTextField.text = self.itemInputText;
            return cell;
        }
        NSString *oilName=@"";
           ICMCarePackageItemModel *itemModel = carePageModel.TT_RECPTION_PACKAGE_ITEM[indexPath.row];
        
        
        for (ICMCarePackageThree_levelItemModel *model in itemModel.OIL_TYPE) {
            if ([model.OIL_VALUE isEqualToString:_oilValue]) {
                oilName=model.OIL_NAME;
                itemModel.OIL_NAME = model.OIL_NAME;
            }
        }
        
        
        
        BOOL ishaveSub=NO;
        if ([itemModel.PACKAGE_ITEM_NAME containsString:@"制动液"]) {
            ishaveSub=YES;
        }
        [cell setupCareItemCellWithSelectedState:[self.careSelectedItemIndexs containsObject:[@(indexPath.row) stringValue]] andTitle:itemModel.PACKAGE_ITEM_NAME isHaveSubTitle:ishaveSub andSubTitle:ishaveSub?@"非出租车/非运营:首次3年后续2年,出租/运营:每50000公里/2年":@"" isLast:NO ishideBtn:itemModel.OIL_TYPE.count>0?NO:YES andValue:oilName];
        
        //isHaveSubTitle?@"非出租车/非运营:首次3年后续2年,出租/运营:每50000公里/2年":
        return cell;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self endEditing:YES];
    BOOL isPackage = tableView == self.carePackageTableView;
    if (isPackage) {
        _oilValue=@"";
        [self.careSelectedItemIndexs removeAllObjects];
        self.itemInputText = @"";
        if (self.packageSelectedIndex == indexPath.row) {
            self.oldSelected = self.packageSelectedIndex;
            self.packageSelectedIndex = -1;
        }else{
            self.packageSelectedIndex = indexPath.row;
            ICMCarePackageModel *carePageModel = self.packages[self.packageSelectedIndex];
            for (NSInteger i=0; i<carePageModel.TT_RECPTION_PACKAGE_ITEM.count; i++) {
                ICMCarePackageItemModel *model = carePageModel.TT_RECPTION_PACKAGE_ITEM[i];
                if (![model.PACKAGE_ITEM_NAME containsString:@"制动液"]) {
                    [self.careSelectedItemIndexs PACA_AddObject:[@(i) stringValue]];
                }
            }
        }
        [self.careItemTableView reloadData];
        [self.carePackageTableView reloadData];
    }else{
        if (self.packageSelectedIndex < 0) {
            return;
        }
        [self.careSelectedItemIndexs containsObject:[@(indexPath.row)stringValue]]?[self.careSelectedItemIndexs removeObject:[@(indexPath.row)stringValue]]:[self.careSelectedItemIndexs PACA_AddObject:[@(indexPath.row)stringValue]];
        [self.careItemTableView reloadData];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
#pragma mark    输入框字符变化
- (void)itemTextFieldHaveChanged:(NSString *)text{
    self.itemInputText = text;
    if (self.packageSelectedIndex>=0) {
        ICMCarePackageModel *carePageModel = self.packages[self.packageSelectedIndex];
        NSInteger count = carePageModel.TT_RECPTION_PACKAGE_ITEM.count;
        if ([text isEqualToString:@""]) {
            if ([self.careSelectedItemIndexs containsObject:[@(count) stringValue]]) {
                [self.careSelectedItemIndexs PACA_RemoveObject:[@(count) stringValue]];
            }
        }else{
            if (![self.careSelectedItemIndexs containsObject:[@(count) stringValue]]) {
                [self.careSelectedItemIndexs PACA_AddObject:[@(count) stringValue]];
            }
        }
        [self.careItemTableView reloadData];
    }
    
}
#pragma mark    小项目状态按钮点击
- (void)itemCellStateButtonAction:(ICMCareItemCell*)cell{
    if (self.packageSelectedIndex<0) {
        return;
    }
    NSInteger index = [self.careItemTableView indexPathForCell:cell].row;
    if ([self.careSelectedItemIndexs containsObject:[@(index) stringValue]]) {
        [self.careSelectedItemIndexs PACA_RemoveObject:[@(index) stringValue]];
    }else{
        [self.careSelectedItemIndexs PACA_AddObject:[@(index) stringValue]];
    }
    [self.careItemTableView reloadData];
}
#pragma mark    获取套餐数据
-(NSMutableArray *)packages{
    if (!_packages) {
        _packages=[NSMutableArray array];
    }
    return _packages;
}
- (void)getCarePackageWithData:(id)data{
    if (data) {
        NSArray *packages = data[@"TT_SET_RECP_PACKAGE"];
        [self.packages removeAllObjects];
        for (NSDictionary *packageDic in packages) {
            ICMCarePackageModel *packageModel = [ICMCarePackageModel createCarePackageModelWithModelDic:packageDic];
            
            [self.packages PACA_AddObject:packageModel];
        }
        dispatch_main_async_safe(^{
            [self changeCarePackageTableViewFrame];
            if (self.isEditType) {
                [self setupCarePackageViewWithTopModel:self.carePackageModel];
                [self setupCarePackageViewWithDownModel:self.carePackageDownModel];
            }
        });
    }
}

- (NSString *)modelCode{
    NSString *code = [self.carTypeView.contentText componentsSeparatedByString:@"|"].firstObject;
    _modelCode = code?:@"";
    return _modelCode;
}


/*************************init*********************/
- (UITableView *)carePackageTableView{
    if (!_carePackageTableView) {
        float orignY = (ActualWidth(350)-50*self.packages.count)>0?(ActualWidth(350)-50*self.packages.count)/2:0;
        float height = (ActualWidth(350)-50*self.packages.count)>0?50*self.packages.count:ActualHeight(350);
        //
        orignY = (ActualWidth(350)-50*5)>0?(ActualWidth(350)-50*5)/2:0;
        height = (ActualWidth(350)-50*5)>0?50*5:ActualHeight(350);
        //
        _carePackageTableView = [[UITableView alloc]initWithFrame:CGRectMake(ActualWidth(75), orignY, ScreenWidth/2-ActualWidth(75), height)];
        _carePackageTableView.delegate = self;
        _carePackageTableView.dataSource = self;
        [_carePackageTableView registerClass:[ICMCarePackageCell class] forCellReuseIdentifier:@"ICMCarePackageCell"];
        _carePackageTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _carePackageTableView.backgroundColor = [UIColor clearColor];
        _carePackageTableView.scrollEnabled = (ActualWidth(350)-50*self.packages.count)>0;
        [self.wholeScrollView addSubview:_carePackageTableView];
    }
    return _carePackageTableView;
}

- (void)changeCarePackageTableViewFrame{
    float orignY = (ActualWidth(350)-50*self.packages.count)>0?(ActualWidth(350)-50*self.packages.count)/2:0;
    float height = (ActualWidth(350)-50*self.packages.count)>0?50*self.packages.count:ActualHeight(350);
    //
    orignY = (ActualWidth(350)-50*5)>0?(ActualWidth(350)-50*5)/2:0;
    height = (ActualWidth(350)-50*5)>0?50*5:ActualHeight(350);
    //
    _carePackageTableView.frame = CGRectMake(ActualWidth(75), orignY, ScreenWidth/2-ActualWidth(75), height);
    [self.careItemTableView reloadData];
    [self.carePackageTableView reloadData];
}


- (UITableView *)careItemTableView{
    if (!_careItemTableView) {
        _careItemTableView = [[UITableView alloc]initWithFrame:CGRectMake(ScreenWidth/2, 0, ScreenWidth/2, ActualWidth(350))];
        _careItemTableView.delegate = self;
        _careItemTableView.dataSource = self;
        [_careItemTableView registerClass:[ICMCareItemCell class] forCellReuseIdentifier:@"ICMCareItemCell"];
        _careItemTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _careItemTableView.backgroundColor = [UIColor clearColor];
        [self.wholeScrollView addSubview:_careItemTableView];
    }
    return _careItemTableView;
}

- (UIView *)easyBreakView{
    if (!_easyBreakView) {
        _easyBreakView = [[UIView alloc]initWithFrame:CGRectMake(0, ActualHeight(350), ScreenWidth, (ScreenHeight-240-ActualHeight(350))/2)];
        _easyBreakView.backgroundColor = BackgroundColor_White;
        UILabel *easyBreakLabel = [[UILabel alloc]initWithFrame:CGRectMake(ActualWidth(15), 0, ActualWidth(40), ActualHeight(60))];
        [easyBreakLabel setUpLabelWithText:@"易损件" alignment:NSTextAlignmentCenter textSize:18 textColor:TitleColor];
        easyBreakLabel.numberOfLines = 0;
        easyBreakLabel.lineBreakMode = NSLineBreakByWordWrapping;
        [_easyBreakView addSubview:easyBreakLabel];
        [easyBreakLabel centerYInSuperView:_easyBreakView];
        [self.wholeScrollView addSubview:_easyBreakView];
    }
    return _easyBreakView;
}

- (UIView *)deepCareView{
    if (!_deepCareView) {
        _deepCareView = [[UIView alloc]initWithFrame:CGRectMake(0, ActualHeight(350)+(ScreenHeight-240-ActualHeight(350))/2, ScreenWidth, (ScreenHeight-240-ActualHeight(350))/2)];
        _deepCareView.backgroundColor = BackgroundColor_White;
        UILabel *deepCareLabel = [[UILabel alloc]initWithFrame:CGRectMake(ActualWidth(15), 0, ActualWidth(40), ActualHeight(60))];
        [deepCareLabel setUpLabelWithText:@"深度养护" alignment:NSTextAlignmentCenter textSize:18 textColor:TitleColor];
        deepCareLabel.numberOfLines = 0;
        deepCareLabel.lineBreakMode = NSLineBreakByWordWrapping;
        [_deepCareView addSubview:deepCareLabel];
        [deepCareLabel centerYInSuperView:_deepCareView];
        [self.wholeScrollView addSubview:_deepCareView];
    }
    return _deepCareView;
}

- (TPKeyboardAvoidingScrollView *)wholeScrollView{
    if (!_wholeScrollView) {
        _wholeScrollView = [[TPKeyboardAvoidingScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-240)];
        [_wholeScrollView contentSizeToFit];
        _wholeScrollView.contentSize = CGSizeMake(ScreenHeight, ScreenHeight-240);
        [self addSubview:_wholeScrollView];
    }
    return _wholeScrollView;
}
#pragma mark    根据model设置界面
- (void)setupCarePackageViewWithTopModel:(ICMCarePackageModel*)topModel{
    
//    self.packages   self.packageSelectedIndex  self.careSelectedItemIndexs
//    topModel.PACKAGE_NAME
    for (NSInteger i=0; i<self.packages.count; i++) {
        ICMCarePackageModel *model = self.packages[i];
        if ([model.PACKAGE_NAME isEqualToString:topModel.PACKAGE_NAME]) {
            self.packageSelectedIndex = i;
        }
    }
    ICMCarePackageModel *model;
    if (self.packages.count) {
        model = self.packages[self.packageSelectedIndex<0?0:self.packageSelectedIndex];
    }
    self.careSelectedItemIndexs  = [NSMutableArray array];
    
    for (ICMCarePackageItemModel *selectedModel in topModel.TT_RECPTION_PACKAGE_ITEM) {
        if (selectedModel.OIL_TYPE.count>0) {
            ICMCarePackageThree_levelItemModel *deepModel=selectedModel.OIL_TYPE[0];
            if (IsStrEmpty(_oilValue)) {
                 _oilValue=deepModel.OIL_VALUE;
            }
           
        }
        for (NSInteger i=0; i<model.TT_RECPTION_PACKAGE_ITEM.count; i++) {
            ICMCarePackageItemModel *itemModel = model.TT_RECPTION_PACKAGE_ITEM[i];
            if ([selectedModel.PACKAGE_ITEM_ID isEqualToString:itemModel.PACKAGE_ITEM_ID]) {
                [self.careSelectedItemIndexs PACA_AddObject:[@(i) stringValue]];
            }
        }
        if ([selectedModel.PACKAGE_ITEM_ID isEqualToString:@"999999"]) {
            [self.careSelectedItemIndexs PACA_AddObject:[@(model.TT_RECPTION_PACKAGE_ITEM.count) stringValue]];
            self.itemInputText = selectedModel.PACKAGE_ITEM_NAME;
        }
    }
    dispatch_main_async_safe(^{
        [self.carePackageTableView reloadData];
        [self.careItemTableView reloadData];
    });
}
//@[@"雨刮",@"刹车片",@"轮胎",@"其他"];
#pragma mark    根据易损件输入选择model设置界面
- (void)setupCarePackageViewWithDownModel:(ICMCarePackageDownModel*)downModel{
    NSArray *easyBreakCodes = @[downModel.CONSUMABLE_WIPER,downModel.CONSUMABLE_BRAKE_BLOCK,downModel.CONSUMABLE_WHEEL,downModel.CONSUMABLE_OTHER];
    NSArray *otherDescs = @[downModel.CONSUMABLE_BRAKE_BLOCK_REMARK,downModel.CONSUMABLE_WHEEL_REMARK,downModel.CONSUMABLE_OTHER_REMARK];
    for (NSInteger i=0; i<self.easyBreakViews.count; i++) {
        ICMSeleteItemView *seleteItemView = self.easyBreakViews[i];
        [seleteItemView setupSelecteButtonWithCode:easyBreakCodes[i]];
        if (i>0) {
            seleteItemView.inputText = otherDescs[i-1];
        }
    }
    NSArray *deepCodes = @[downModel.ENGINE_LUBRICATION,downModel.FUEL_SYSTEM_MAINTENANCE,downModel.AIR_SYSTEM_MAINTENANCE,downModel.AIR_CONDITION_MAINTENANCE];
    for (NSInteger i=0; i<self.deepCareViews.count; i++) {
        ICMSeleteItemView *seleteItemView = self.deepCareViews[i];
        [seleteItemView setupSelecteButtonWithCode:deepCodes[i]];
    }
    
}
#pragma mark    数据输出
- (BOOL)isHave_Wiper{
    ICMSeleteItemView *selecteView = self.easyBreakViews[0];
    return selecteView.isSelected;
}

- (BOOL)isHave_Brake_block{
    ICMSeleteItemView *selecteView = self.easyBreakViews[1];
    return selecteView.isSelected;
}

- (NSString *)BRAKE_BLOCK_REMARK{
    ICMSeleteItemView *selecteView = self.easyBreakViews[1];
    return selecteView.inputText;
}

- (BOOL)isHave_Wheel{
    ICMSeleteItemView *selecteView = self.easyBreakViews[2];
    return selecteView.isSelected;
}

- (NSString *)WHEEL_REMARK{
    ICMSeleteItemView *selecteView = self.easyBreakViews[2];
    return selecteView.inputText;
}

- (BOOL)isHave_Other{
    ICMSeleteItemView *selecteView = self.easyBreakViews[3];
    return selecteView.isSelected;
}

- (NSString *)Other_REMARK{
    ICMSeleteItemView *selecteView = self.easyBreakViews[3];
    return selecteView.inputText;
}

- (BOOL)isHave_Lubrication{
    ICMSeleteItemView *selecteView = self.deepCareViews[0];
    return selecteView.isSelected;
}

- (BOOL)isHave_FUEL_SYSTEM_MAINTENANCE{
    ICMSeleteItemView *selecteView = self.deepCareViews[1];
    return selecteView.isSelected;
}

- (BOOL)isHave_AIR_SYSTEM_MAINTENANCE{
    ICMSeleteItemView *selecteView = self.deepCareViews[2];
    return selecteView.isSelected;
}

- (BOOL)isHave_AIR_CONDITION_MAINTENANCE{
    ICMSeleteItemView *selecteView = self.deepCareViews[3];
    return selecteView.isSelected;
}

- (NSMutableArray *)careSelectedItems{
    _careSelectedItems = [NSMutableArray array];
    ICMCarePackageModel *carePackModel;
    if(self.packages.count){
        carePackModel = self.packages[self.packageSelectedIndex<0?self.oldSelected:self.packageSelectedIndex];
    }
    for (NSString *itemIndex in self.careSelectedItemIndexs) {
        NSInteger index = [itemIndex integerValue];
        if (index == carePackModel.TT_RECPTION_PACKAGE_ITEM.count) {
            ICMCareItemCell *cell = [self.careItemTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
            ICMCarePackageItemModel *itemModel = [[ICMCarePackageItemModel alloc]init];
            itemModel.PACKAGE_ITEM_ID = @"999999";
            itemModel.PACKAGE_NAME = carePackModel.PACKAGE_NAME;
            itemModel.PACKAGE_ITEM_NAME = cell.itemTextField.text;
            [_careSelectedItems PACA_AddObject:itemModel];
        }else{
            ICMCarePackageItemModel *itemModel = carePackModel.TT_RECPTION_PACKAGE_ITEM[index];
            [_careSelectedItems PACA_AddObject:itemModel];
        }
    }
    for (ICMCarePackageItemModel *model in _careSelectedItems) {
        if (model.OIL_TYPE.count>0) {
            NSMutableArray *arr=[NSMutableArray array];
            if (![_oilValue isEqualToString:@""]) {
                for (ICMCarePackageThree_levelItemModel *threemodel in model.OIL_TYPE) {
                    if ([threemodel.OIL_VALUE isEqualToString:_oilValue]) {
                        [arr addObject:threemodel];
                    }

                }
            }
            model.OIL_TYPE=arr;
        }
    }
    return _careSelectedItems;
}
#pragma mark 中断原因弹框
-(void)popCancelResonWithindex:(NSInteger)index{
    ICMCarePackageModel *carePageModel = self.packages[self.packageSelectedIndex<0?self.oldSelected:self.packageSelectedIndex];
    ICMCarePackageItemModel *itemModel = carePageModel.TT_RECPTION_PACKAGE_ITEM[index];
          self.choseArr=[itemModel.OIL_TYPE mutableCopy];
    
    ChoseMenuViewController *choseMenu = [[ChoseMenuViewController alloc] init];
    ICMCareItemCell *cell=[self.careItemTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
    choseMenu.modalPresentationStyle = UIModalPresentationPopover;
    choseMenu.popoverPresentationController.sourceView = cell.itemButton;
    choseMenu.popoverPresentationController.sourceRect =CGRectMake(0, 0,cell.itemButton.width,cell.itemButton.height);
    choseMenu.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
    CGFloat height =self.choseArr.count>5?220:44*self.choseArr.count;
    choseMenu.displaySize=CGSizeMake(cell.itemButton.width,height);
    choseMenu.choseArr = self.choseArr;
    choseMenu.delegate = self;
    [self.getcarCtr presentViewController:choseMenu animated:YES completion:nil];
}
#pragma mark ChoseMenuViewControllerDelegate
-(void)MenuDidClickIndex:(NSInteger)index{
    ICMCarePackageThree_levelItemModel *model=self.choseArr[index];
    _oilValue=model.OIL_VALUE;

    [self.careItemTableView reloadData];
    
}

@end





